package com.vodden.money.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.vodden.money.shared.Entry;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class money_vodden_com implements EntryPoint {
	private LedgerServiceAsync ledgerService;
	private CellTable<Entry> ledgerTable;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		/* Create a cell table */
		ledgerTable = new CellTable<Entry>();
		ledgerTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		DateCell dateCell = new DateCell(DateTimeFormat.getFormat("dd/MM/yy"));
		Column<Entry, Date> dateColumn = new Column<Entry, Date>(dateCell) {
			@Override
			public Date getValue(Entry entry) {
				return entry.getDate();
			}
		};
		ledgerTable.addColumn(dateColumn, "Date");

		/* create description column */
		TextColumn<Entry> descriptionColumn = new TextColumn<Entry>() {
			@Override
			public String getValue(Entry entry) {
				return entry.getDescription();
			}
		};
		ledgerTable.addColumn(descriptionColumn, "Description");

		/* create account column */
		TextColumn<Entry> accountColumn = new TextColumn<Entry>() {
			@Override
			public String getValue(Entry entry) {
				return entry.getAccountName();
			}
		};
		ledgerTable.addColumn(accountColumn, "Account");

		/* create value column */
		NumberCell debitCell = new NumberCell(
				NumberFormat.getFormat("#,##0.00"));
		Column<Entry, Number> debitColumn = new Column<Entry, Number>(
				debitCell) {
			@Override
			public Integer getValue(Entry entry) {
				Integer value = entry.getValue();
				if (value <= 0)
					return value / 10;
				else
					return null;
			}
		};
		debitColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		ledgerTable.addColumn(debitColumn, "Debit");

		/* create value column */
		NumberCell creditCell = new NumberCell(
				NumberFormat.getFormat("#,##0.00"));
		Column<Entry, Number> creditColumn = new Column<Entry, Number>(
				creditCell) {
			@Override
			public Integer getValue(Entry entry) {
				Integer value = entry.getValue();
				if (value >= 0)
					return value / 10;
				else
					return null;
			}
		};
		creditColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		ledgerTable.addColumn(creditColumn, "Credit");

		NumberCell balanceCell = new NumberCell(
				NumberFormat.getFormat("#,##0.00"));
		Column<Entry, Number> balanceColumn = new Column<Entry, Number>(
				balanceCell) {
			@Override
			public Integer getValue(Entry entry) {
				// TODO Fix Balance Calculations
				return 10;
			}
		};
		balanceColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		ledgerTable.addColumn(balanceColumn, "Balance");
		
		getLedgerEntries();

		RootPanel.get().add(ledgerTable);
	}

	private void getLedgerEntries() {

		// spin up the ledgerService proxy
		if (ledgerService == null) {
			ledgerService = GWT.create(LedgerService.class);
		}

		// register the call back object
		AsyncCallback<ArrayList<Entry>> callback = new AsyncCallback<ArrayList<Entry>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Do something with the nasty errors!
			}

			@Override
			@SuppressWarnings("unchecked")
			public void onSuccess(ArrayList<Entry> entries) {
				ledgerTable.setRowData((List<Entry>) entries.clone());
			}
		};

		ledgerService.getLedgerEntries(callback);
	}
}
